<?php

namespace App\Http\Controllers;

use App\Models\TuanOrder;
use Exception;
use Illuminate\Http\Request;

/**
 * 订单相关
 */
class OrderController extends Controller
{
    /**
     * 订单列表
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function order_list(Request $request)
    {
        try {
            $status = 0;
            $data   = [];
            $userID = $request->get('user_id');
            if (empty($userID)) {
                throw new Exception('缺少参数');
            }
            $type = $request->get('type') ?? 5;

            switch ($type) {
                case 1: //待付款订单
                    $orders = TuanOrder::where([
                        'user_id' => $userID, 'order_type' => 1, 'pay_status' => 1, 'order_status' => 0,
                    ])->paginate(10);
                    break;
                case 2: //待使用订单
                    $orders = TuanOrder::where([
                        'user_id' => $userID, 'pay_status' => 2, 'order_status' => 1,
                    ])->paginate(10);
                    break;
                case 3: //待评价订单
                    $orders = TuanOrder::where([
                        'user_id' => $userID, 'pay_status' => 2, 'order_status' => 2,
                    ])->paginate(10);
                    break;
                case 4: //退款售后
                    $orders = TuanOrder::with(['tuan_code' => function ($query) {
                        $query->where('is_used', 2)->whereIn('status', [2, 3, 4]);
                    }])->where(['user_id' => $userID, 'order_type' => 1])->paginate(10);
                    break;
                case 5: //全部
                default:
                    $orders = TuanOrder::where(['user_id' => $userID, 'pay_status' => 2])
                        ->where('order_status', '<>', 4)->paginate(10);
                    break;
            }
            //处理数据
            foreach ($orders as $key => $value) {
                $data[$key]['order_id']     = $value->order_id;
                $data[$key]['tuan_id']      = $value->tuan_id;
                $data[$key]['shop_id']      = $value->shop_id;
                $data[$key]['order_type']   = $value->order_type; //1为团购订单2为优惠买单3为充值订单
                $data[$key]['total_price']  = sprintf('%.2f', $value->total_price / 100); //原价格
                $data[$key]['price']        = sprintf('%.2f', $value->price / 100); //实付价格
                $data[$key]['pay_status']   = $value->pay_status; //支付状态 1未支付 2已支付 -1退款中 -2退款完成
                $data[$key]['order_status'] = $value->order_status; //订单状态 1待使用 2待评价 3已评价 4已取消
                $data[$key]['tuan_code']    = [];
                if (count($value->tuan_code) > 0) {
                    $code = [];
                    foreach ($value->tuan_code as $k => $v) {
                        $code[$k]['code']          = $v->code;
                        $code[$k]['real_money']    = sprintf('%.2f', $v->real_money / 100); //真实花费的MONEY
                        $code[$k]['real_integral'] = $v->real_integral; //真实积分
                        $code[$k]['is_used']       = $v->is_used; //1代表已经使用 2未使用
                        $code[$k]['status']        = $v->status; //1代表正常，2代表申请退款中，3代表退款审核通过，4退款成功
                        $code[$k]['is_comment']    = $v->is_comment; //是否点评,0未评价,1已评价
                    }
                    $data[$key]['tuan_code'] = $code;
                }
                $data[$key]['tuan_info'] = [];
                if (count($value->tuan_info) > 0) {
                    $info                    = [];
                    $info['tuan_name']       = $value->tuan_info->title;
                    $info['tuan_img']        = $value->tuan_info->photo;
                    $data[$key]['tuan_info'] = $info;
                }
                $data[$key]['shop'] = [];
                if (count($value->shop) > 0) {
                    $shop               = [];
                    $shop['shop_name']  = $value->shop->shop_name;
                    $shop['shop_logo']  = $value->shop->logo;
                    $data[$key]['shop'] = $shop;
                }
                $data[$key]['active'] = [];
                if (count($value->active) > 0) {
                    $active               = [];
                    $active['act_id']     = $value->active->act_id; //活动类型id 1积分秒杀 2积分优惠 3团购立减 4双倍积分
                    $active['integral']   = $value->active->integral; //积分
                    $active['price']      = sprintf('%.2f', $value->active->price / 100); //价格
                    $active['status']     = $value->active->status; //0-未审核 1-审核通过 2-审核不通过
                    $data[$key]['active'] = $active;
                }
            }

            $status = 1;
            $msg    = '查询成功';
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return response()->json(['status' => $status, 'msg' => $msg, 'data' => $data]);
    }

    /**
     * 订单详情
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function order_info(Request $request)
    {
        try {
            $status   = 0;
            $data     = [];
            $order_id = $request->get('order_id');
            if (empty($order_id)) {
                throw new Exception('缺少参数');
            }
            $order = TuanOrder::where(['order_id' => $order_id])->first();
            //处理数据
            $data['order_id']  = $order->order_id;
            $data['order_num'] = $order->order_num;
            if (count($order->user) > 0) {
                $data['phone'] = $order->user->phone;
            }
            $data['create_time']  = date('Y-m-d H:i:s', $order->create_time);
            $data['order_type']   = $order->order_type; //1为团购订单2为优惠买单3为充值订单
            $data['total_price']  = sprintf('%.2f', $order->total_price / 100); //原价格
            $data['price']        = sprintf('%.2f', $order->price / 100); //实付价格
            $data['pay_status']   = $order->pay_status; //支付状态 1未支付 2已支付 -1退款中 -2退款完成
            $data['order_status'] = $order->order_status; //订单状态 1待使用 2待评价 3已评价 4已取消
            $data['tuan_code']    = [];
            if (count($order->tuan_code) > 0) {
                $code = [];
                foreach ($order->tuan_code as $k => $v) {
                    $code[$k]['code']          = $v->code;
                    $code[$k]['real_money']    = sprintf('%.2f', $v->real_money / 100); //真实花费的MONEY
                    $code[$k]['real_integral'] = $v->real_integral; //真实积分
                    $code[$k]['is_used']       = $v->is_used; //1代表已经使用 2未使用
                    $code[$k]['status']        = $v->status; //1代表正常，2代表申请退款中，3代表退款审核通过，4退款成功
                    $code[$k]['is_comment']    = $v->is_comment; //是否点评,0未评价,1已评价
                }
                $data['tuan_code'] = $code;
            }
            $data['tuan_info'] = [];
            if (count($order->tuan_info) > 0) {
                $info              = [];
                $info['tuan_name'] = $order->tuan_info->title;
                $info['tuan_img']  = $order->tuan_info->photo;
                $data['tuan_info'] = $info;
            }
            $data['shop'] = [];
            if (count($order->shop) > 0) {
                $shop              = [];
                $shop['shop_name'] = $order->shop->shop_name;
                $shop['shop_logo'] = $order->shop->logo;
                $shop['tel']       = $order->shop->tel;
                $shop['discount']  = $order->shop->discount;
                $data['shop']      = $shop;
            }
            $data['combos'] = [];
            if (count($order->combos) > 0) {
                $comobs = [];
                foreach ($order->combos as $k => $v) {
                    $combos[$k]['name']       = $v->name;
                    $combos[$k]['combo_info'] = $v->combo_info;
                }
                $data['combos'] = $combos;
            }
            $data['active'] = [];
            if (count($order->active) > 0) {
                $active             = [];
                $active['act_id']   = $order->active->act_id;
                $active['integral'] = $order->active->integral;
                $active['price']    = sprintf('%.2f', $order->active->price / 100);
                $active['status']   = $order->active->status;
                $data['active']     = $active;
            }

            $status = 1;
            $msg    = '查询成功';
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return response()->json(['status' => $status, 'msg' => $msg, 'data' => $data]);
    }
}
